﻿using MediatR;
using Traceability.App.Api.Application.Command.Detail;
using Traceability.App.Api.DTOs;
using Traceability.App.ErrorCount;
using Traceability.App.InfrasIntructure;
using Traceability.App.Model;
using Traceability.App.Model.Basic;

namespace Traceability.App.Api.Application.Handler.Detail
{
    public class GetFanBasicHandler : IRequestHandler<GetFanBasicCommand, ApiResult<BasicDTO>>
    {
        IBaseRepository<BasicModel> basicRepo;
        IBaseRepository<StoreModel> storeREpo;

        public GetFanBasicHandler(IBaseRepository<BasicModel> basicRepo, IBaseRepository<StoreModel> storeREpo)
        {
            this.basicRepo = basicRepo;
            this.storeREpo = storeREpo;
        }

        public Task<ApiResult<BasicDTO>> Handle(GetFanBasicCommand request, CancellationToken cancellationToken)
        {
            ApiResult<BasicDTO> res = new ApiResult<BasicDTO>();
            var basics = basicRepo.GetAll().Where(c => !c.IsDeleted &&  c.Batch==request.Batch);
            var stores = storeREpo.GetAll().Where(c => !c.IsDeleted);
            var linq = (from a in basics
                       join b in stores on a.Sid equals b.Id
                       select new BasicDTO
                       {
                           Id = a.Id,
                           Sid = b.Id,
                           Batch = a.Batch,
                           Expiration = a.Expiration,
                           License = a.License,
                           Company = a.Company,
                           Picture = a.Picture,
                           StoreName = b.StoreName,
                       }).FirstOrDefault();
          
            res.Datas = linq;
            res.Msg = "反填基本信息成功";
            res.Code=ApiEnum.Success;
            return Task.FromResult(res);
        }
    }
}
